/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-06-28 20:14:33
 * @LastEditTime: 2022-06-28 22:16:41
 * @LastEditors: liutq
 * @Reference:
 */
package repo

import (
	"time"

	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/model"
)

type MyCollect struct {
	ID              uint      `gorm:"column:id; parimary_key" json:"id"`
	OwnerUid        uint      `gorm:"column:owner_uid" json:"owner_uid" `
	HistoryOwner    uint      `gorm:"column:history_owner" json:"history_owner" `
	GoodsKind       uint8     `gorm:"column:goods_kind" json:"goods_kind" `
	GoodsId         uint      `gorm:"column:goods_id" json:"goods_id" `
	GoodsCode       string    `gorm:"column:goods_code" json:"goods_code" `
	CertificatId    string    `gorm:"column:certificat_id" json:"certificat_id" `
	CertificatHash  string    `gorm:"column:certificat_hash" json:"certificat_hash" `
	CertificatTiime time.Time `gorm:"column:certificat_tiime" json:"certificat_tiime" `
	BlockHeight     uint      `gorm:"column:block_height" json:"block_height" `
	CollectTime     time.Time `gorm:"column:collect_time" json:"collect_time" `
	CollectType     uint8     `gorm:"column:collect_type" json:"collect_type" `
	CreateTime      time.Time `gorm:"column:create_time" json:"create_time" `
	IsOpen          uint8     `gorm:"column:is_open" json:"is_open" `
	DeleteTag       uint8     `gorm:"column:delete_tag" json:"delete_tag" `
	GoodsType       uint8     `gorm:"column:goods_type" json:"goods_type" `
	RepoStatus      uint8     `gorm:"column:repo_status" json:"repo_status" `
	RepoStartTime   time.Time `gorm:"column:repo_start_time" json:"repo_start_time" `
	SmsNotice       uint8     `gorm:"column:sms_notice" json:"sms_notice" `
	SerialNumber    string    `gorm:"column:serial_number" json:"serial_number" `
}

type MyCollectDetail struct {
	ID              uint      `gorm:"column:id; parimary_key" json:"id"`
	GoodsId         uint      `gorm:"column:goods_id" json:"goods_id"`
	GoodsNum        uint      `gorm:"column:goods_num" json:"goods_num"`
	GoodsName       string    `gorm:"column:goods_name" json:"goods_name"`
	GoodsListImg    string    `gorm:"column:goods_list_img" json:"goods_list_img"`
	GoodsCode       string    `gorm:"column:goods_code" json:"goods_code"`
	OwnerUid        uint      `gorm:"column:owner_uid" json:"owner_uid" `
	CertificatId    string    `gorm:"column:certificat_id" json:"certificat_id" `
	CertificatHash  string    `gorm:"column:certificat_hash" json:"certificat_hash" `
	CertificatTiime time.Time `gorm:"column:certificat_tiime" json:"certificat_tiime" `
	BlockHeight     uint      `gorm:"column:block_height" json:"block_height" `
	CollectTime     time.Time `gorm:"column:collect_time" json:"collect_time" `
	GoodsType       uint8     `gorm:"column:goods_type" json:"goods_type" `
	RepoStatus      uint8     `gorm:"column:repo_status" json:"repo_status" `
	RepoStartTime   time.Time `gorm:"column:repo_start_time" json:"repo_start_time" `
	GoodsRepoPrice  float64   `gorm:"column:goods_repo_price" json:"goods_repo_price"`
}

func UpdateMyCollectForRepo(mycollect_ *common.Mycollect) error {
	if err := model.Db.Where(&MyCollect{GoodsId: mycollect_.GoodsId}).Update(&mycollect_).Error; err != nil {
		return err
	}
	return nil
}

func UpdateMyCollect(mycollect_ *MyCollect) error {
	if err := model.Db.Model(&MyCollect{}).Update(&mycollect_).Error; err != nil {
		return err
	}
	return nil
}

func GetUserCollectDetailById(userId_ uint, id_ uint) *MyCollectDetail {
	var mycollect MyCollectDetail

	model.Db.Table("my_collect").Select("my_collect.*,user.username,user.headimg, goods.goods_name,goods.goods_list_img,goods_repo_price").Joins("inner join goods on goods.id=my_collect.goods_id").Joins("inner join user on user.id = my_collect.owner_uid").Where(&MyCollect{ID: id_, OwnerUid: userId_, GoodsKind: 1, DeleteTag: 0}).Find(&mycollect)

	return &mycollect
}
